package io.zso.commons.dynamic.datasource.mongo.aspect;

import io.zso.commons.tools.exception.RenException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hp@zso.io
 */
@Configuration
public class ConfigurableAdvisorConfig {

    @Value("${zso.mongodb.dynamic.execution}")
    private String pointcut;

    @Bean
    public AspectJExpressionPointcutAdvisor configurableAdvisor() {
        if (StringUtils.isEmpty(this.pointcut)) {
            throw new RenException("dynamic mongo datasource need set pointcut execution: 'zso.mongodb.dynamic.execution'");
        }
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression(pointcut);
        advisor.setAdvice(new DynamicMongoDataSourceAdvice());
        return advisor;
    }
}